/*
 * @Author: duanguang
 * @Date: 2022-03-03 09:37:35
 * @LastEditTime: 2022-04-29 14:10:15
 * @LastEditors: duanguang
 * @Description: 
 * @FilePath: /legions-server/app/service/branch_merge_request.ts
 * 「扫去窗上的尘埃，才可以看到窗外的美景。」
 */

import { Service } from 'egg';
import { PageModel } from '@/model/responseModel';
import { auditStatusEnum, UserRoleEnum } from '@/constants/enum.types';
import { BranchMergeRequest } from '@/model/branch_merge_request';
export default class BranchMergeRequestService extends Service {
    

    /**
     * 新增合并分支请求
     */
    public async create(params: Partial<BranchMergeRequest>) {
        /** 新增 */
        try {
            return await this.ctx.model.BranchMergeRequest.create({
                ...params,
            } as BranchMergeRequest);
        } catch (error) {
            this.logger.error(error);
            throw error;
        }
    }
    /** 根据id查询删除项目 */
    public async delete(id: string,userRule: Number,
        useruid:string): Promise<boolean> {
        try {
            const deleteResult = await this.ctx.model.BranchMergeRequest.deleteOne({
                _id: id,
                ...userRule > UserRoleEnum.admin&&{creator_id:useruid}
            });
            if (deleteResult.deletedCount) {
                return true
            }
            return false
        } catch (error) {
            this.logger.error(error);
            throw error;
        }
    }
    public async reject(id: string,userRule: Number,userid: string) {
        try {
            const result = await this.ctx.model.BranchMergeRequest.updateOne({
                _id: id,
                ...userRule > UserRoleEnum.admin&&{assign_members_id:userid}
            },{
                audit_status:auditStatusEnum.reject
            });
            if (result.nModified) {
                return true
            }
            return false
        } catch (error) {
            this.logger.error(error);
            throw error;
        }
    }
    public async approved(params: {
        id: string,
        userId:string
    }) {
        if (params.id) {
            try {
                const { userId} = params
                const res = await this.ctx.model.BranchMergeRequest.updateOne({
                    _id: params.id,
                    $or: [ { assign_members_id: userId }],
                    ...{audit_status: auditStatusEnum.waiting}
                },{
                    audit_status:auditStatusEnum.pass,
                });
                if (res.nModified) {
                    return true
                }
                return false
            }
            catch (error) {
                
            }
        }
    }
    public async edit(params: Partial<BranchMergeRequest> & {
        id: string,
        userId:string
    }) {
        if (params.id) {
            try {
                const { userId,id,...props } = params
                const res = await this.ctx.model.BranchMergeRequest.updateOne({
                    _id: params.id,
                    $or: [ { creator_id: userId }, { assign_members_id: userId }],
                    ...{audit_status: auditStatusEnum.waiting}
                },{
                    ...props,
                });
                if (res.nModified) {
                    return true
                }
                return false
            } catch (error) {
                
            }
        }
    }
    public async getList(params: {
        /** 当前页 */
        pageIndex: string;
        /** 每页条数 */
        pageSize: string;
        /** 来源分支 */
        source_branch?: string;
        /** 仓库名称 */
        repository_name?: string;
        /** 处理状态 */
        audit_status?: string;
        userRule: Number;
        userId: string;
    }) {
        try {
            const repository_name = params.repository_name || ''
            const source_branch = params.source_branch || ''
            const audit_status = params.audit_status || ''
            const pageIndex = parseInt(params.pageIndex,10)
            const pageSize = parseInt(params.pageSize,10)
            const condition = {
                $match: {
                    ...repository_name.trim() && { repository_name: { $regex: repository_name } },
                    ...source_branch.trim() && { source_branch: { $regex: source_branch } },
                    ...audit_status.trim() && { audit_status: audit_status },
                    $or:[{creator_id:params.userId},{assign_members_id:params.userId}],
                }
            }
            const list = await this.ctx.model.BranchMergeRequest.aggregate([
                {
                    ...condition
                },
                {
                    $sort: {
                        update_time: -1,
                    },
                },
                {
                    $skip: (pageIndex - 1) * pageSize,
                },
                {
                    $limit: pageSize,
                },
            ])
            const total: number = await this.ctx.model.BranchMergeRequest.find(condition.$match).count();
            return new PageModel<BranchMergeRequest>({
                list,
                pageIndex: pageIndex || 1,
                pageSize: pageSize || 10,
                total,
            });
        } catch (error) {
            this.logger.error(error);
            throw error;
        }
    }
}
